package page.sample.administratorpackage;

import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.markup.repeater.data.DataView;
import org.apache.wicket.markup.repeater.data.ListDataProvider;

import page.sample.AuthorizationPage;
import dao.impl.GoodsDao;
import entity.Administrator;
import entity.Goods;

public class ShowGoodsPage extends WebPage {

	public ShowGoodsPage(final Administrator administrator) {
		final GoodsDao goodsDao = new GoodsDao(
				app.MyFirstWebApplication.DATA_XML_FOLDER);

		add(new DataView<Goods>("table-rows", new ListDataProvider<Goods>(
				goodsDao.getAll())) {
			@Override
			protected void populateItem(final Item<Goods> item) {
				final Goods goods = item.getModelObject();
				// add data for table cell in row
				item.add(new Label("name", goods.getName()));
				item.add(new Label("price", goods.getPrice()));
				
				item.add(new Link("deleteGoods") {
					@Override
					public void onClick() {
						
						goodsDao.delete(goods.getName());
						
						setResponsePage(new ShowGoodsPage(administrator));
					}
					});
			}
		});
		
		add(new Link("showUsers") {

			@Override
			public void onClick() {
				setResponsePage(new ShowUsers(administrator));
			}

		});
		
		add(new Link("addGoods")
		{

			@Override
			public void onClick() {
				setResponsePage(new AddGoodsPage(administrator));
				
			}
			
		});

		add(new Link("showBlackList") {

			@Override
			public void onClick() {
				setResponsePage(new BlackListPage(administrator));
			}

		});
		
		add(new Link("showGoods")
		{

			@Override
			public void onClick() {
				setResponsePage(new ShowGoodsPage(administrator));
				
			}
			
		});

		add(new Link("exit") {

			@Override
			public void onClick() {
				setResponsePage(new AuthorizationPage());

			}

		});
	}
}
